package com.coollang.smartbra.presenter;

import android.os.Handler;

import com.coollang.smartbra.activity.NewActionActivity;
import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.fragment.NewActionCountFragment;
import com.coollang.smartbra.utils.ActionTypeParse;

import java.util.ArrayList;
import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * @author: Timmy
 * @data: 2017/12/15
 * @Fuction:
 */

public class NewActionCountFragmentPresenter {

    private NewActionCountFragment mFragment;
    private Handler mHandler = new Handler();
    private int mProgress;

    private static final int CORRECT_FLAG = 1;
    private static final int ERROR_FLAG = 2;
    private static final int ERROR_TYPE = 3;
    private HashMap<Integer, Integer> mActionCountMap = new HashMap();
    private ArrayList<NewActionData> mActionDatas = new ArrayList<>();

    public NewActionCountFragmentPresenter(NewActionCountFragment countFragment) {
        mFragment = countFragment;
        initData();
    }

    private void initData() {
        mActionCountMap.put(CORRECT_FLAG, 0);
        mActionCountMap.put(ERROR_FLAG, 0);
    }

    public void onDestroyView() {
        mHandler.removeCallbacks(mRunnable);
    }

    public void startCount() {
        mHandler.post(mRunnable);
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (mProgress < NewActionCountFragment.PROGRESSBAR_MAX) {
                // 小于1000
                mProgress += 10;
                mFragment.setCircularSeekBarProgress(mProgress);
                mHandler.postDelayed(this, 1);
            }
        }
    };

    public int getProgress() {
        return mProgress;
    }

    public void setProgress(int progress) {
        mProgress = progress;
    }

    public void removeHandler() {
        mHandler.removeCallbacks(mRunnable);
    }

    public void processCountData(int heartRate, int actionFlag, int errorType) {

        NewActionData actionData = new NewActionData();
        int actionType = mFragment.getActionType();
        actionData.setActionType(actionType);
        actionData.setStartTime(System.currentTimeMillis());

        actionData.setHeartRate(heartRate);
        actionData.setActionFlag(actionFlag);
        actionData.setErrorType(errorType);
        mActionDatas.add(actionData);

        // 统计正确错误个数
        mActionCountMap.put(actionFlag, mActionCountMap.get(actionFlag) + 1);
        // 获取正确的个数
        int correctCount = mActionCountMap.get(CORRECT_FLAG);
        // 获取错误的个数
        int errorCount = mActionCountMap.get(ERROR_FLAG);
        mFragment.setCorrectCount(correctCount);
        float count = correctCount + errorCount;
        int correctPercentage = (int) ((correctCount / count) * 100);
        mFragment.setProgress(correctPercentage);

        // 播报语音项目
        if (actionFlag == CORRECT_FLAG) {
            actionType = -1;
        }

        int actionVoice = ActionTypeParse.getActionVoice(actionType, errorType);
        ((NewActionActivity) mFragment.getContext()).playVoice(actionVoice);

        // eventbus 通知详情页面
        EventBus.getDefault().post(actionData);
    }

    public ArrayList<NewActionData> getActionDetails() {
        return mActionDatas;
    }
}
